package com.njtoyo.taxi.admin.library.payment.jyb;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import com.google.gson.JsonObject;
import com.njtoyo.taxi.admin.library.payment.common.PayBasicInfo;
import com.njtoyo.taxi.admin.library.payment.jyb.util.HttpClient;
import com.njtoyo.taxi.admin.library.payment.jyb.util.JYBCerts;
import com.njtoyo.taxi.admin.library.payment.jyb.util.JYBString;
import lombok.extern.slf4j.Slf4j;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

/**
 * @Author Dell
 * @Date 2021/6/1 9:31
 */
@Component
@Slf4j
public class JybPayClient {

    @Autowired
    private JybPayConfig jybPayConfig;

    public boolean refund(PayBasicInfo payBasicInfo) {
        JybRefundOrder order = buildDefaultRefundOrder(payBasicInfo);
        String certId = JYBCerts.getSignCertId(jybPayConfig.getKeyPath(), jybPayConfig.getKeyPass());
        order.setCertId(certId);
        String toSignStr = JYBString.getSignStr(order);
        String signature = JYBString.getSignVerify(toSignStr, jybPayConfig.getKeyPath(), jybPayConfig.getKeyPass());
        order.setSignature(signature);
        String json = Json.toJson(order, JsonFormat.compact());
        String encodeJson = Base64.encode(json);
        String result = HttpClient.sendPost(jybPayConfig.getRefundUrl(), "params=" + encodeJson, "application/x-www-form-urlencoded");
        if ("error".equalsIgnoreCase(result)) {
            throw new RuntimeException("refund error");
        }
        JsonObject jsonObject = JYBString.stringToJson(result);
        log.info("农商行接口返回：" + jsonObject);
        return JYBString.parseRefundResponse(jsonObject);
    }

    private JybRefundOrder buildDefaultRefundOrder(PayBasicInfo payBasicInfo) {
        JybRefundOrder order = new JybRefundOrder();

        if (Objects.nonNull(payBasicInfo.getWxAppType())) {
            switch (payBasicInfo.getWxAppType()) {
                case wxapp:
                    order.setMerId(jybPayConfig.getMerIdWxApp());
                    break;
                case wxmina:
                    order.setMerId(jybPayConfig.getMerIdWxmina());
                    break;
                default:
                    break;
            }
        }

        order.setVersion("3.0.0");
        order.setCharset("utf-8");
        order.setLocale("en");
        order.setOrderAmount(String.format("%012d", payBasicInfo.getRefundAmount().multiply(BigDecimal.valueOf(100)).intValue()));
        order.setOrderTime(DateUtil.format(new Date(), "yyyyMMddHHmmss"));
//        order.setOrderNumber(NumberGenerator.generate(NumberGenerator.REFUND_NUMBER));
        order.setOrderNumber(payBasicInfo.getRufundNo());
        order.setInitOrderNumber(payBasicInfo.getNumber());
        order.setInitOrderTime(DateUtil.format(payBasicInfo.getCreatedAt(), "yyyyMMddHHmmss"));
        order.setTransType("34");
        order.setChannel("6001");
        order.setMchtAccessType("01");
        order.setPayAccessTypeId("02");
        order.setCurrencyType("156");
        order.setCustomerIp("116.62.36.151");
        order.setMerType("3");
        order.setInterType("T02");
        return order;
    }

}
